Namespace SelectChar

    Module mSelChar

#Region "Selection Vars"

        Const cSelectAnimTick As Long = 1150000

        Private Enum SelectAnims
            aUnSelected = 0
            aSelect = 1
            aSelected = 2
            aUnSelect = 3
        End Enum

        Dim WithEvents SelectWin As GUI.MirWindow

        Dim WithEvents btnNew As GUI.MirButton
        Dim WithEvents btnDelete As GUI.MirButton
        Dim WithEvents btnStart As GUI.MirButton
        Dim WithEvents btnExit As GUI.MirButton

        Dim WithEvents aSelChar1Norm As GUI.MirAnimation
        Dim WithEvents aSelChar2Norm As GUI.MirAnimation
        Dim WithEvents aSelChar1Shdw As GUI.MirAnimation
        Dim WithEvents aSelChar2Shdw As GUI.MirAnimation
        Dim WithEvents aSelChar1Magc As GUI.MirAnimation
        Dim WithEvents aSelChar2Magc As GUI.MirAnimation

        Dim selAnimState1 As Integer
        Dim selAnimState2 As Integer

        Dim WithEvents lblCharInfo As GUI.MirText

#End Region

#Region "Creation Vars"

        Const cCreateAnimTick As Long = 1500000

        Dim WithEvents CreateWin As GUI.MirWindow

        Dim WithEvents lblClassInfo As GUI.MirText

        Public WithEvents btnClassWar As GUI.MirButton
        Public WithEvents btnClassSor As GUI.MirButton
        Public WithEvents btnClassTao As GUI.MirButton
        Public WithEvents btnClassSin As GUI.MirButton

        Dim WithEvents btnCreateOK As GUI.MirButton
        Dim WithEvents btnCreateCancel As GUI.MirButton

        Dim WithEvents aCreateMaleShdw As GUI.MirAnimation
        Dim WithEvents aCreateFemaleShdw As GUI.MirAnimation
        Dim WithEvents aCreateMaleNorm As GUI.MirAnimation
        Dim WithEvents aCreateFemaleNorm As GUI.MirAnimation
        Dim WithEvents aCreateMaleMagc As GUI.MirAnimation
        Dim WithEvents aCreateFemaleMagc As GUI.MirAnimation

        Dim WithEvents aCreateStoneNorm As GUI.MirAnimation
        Dim WithEvents aCreateStoneShdw As GUI.MirAnimation

        Dim WithEvents txtCreateName As GUI.MirTextBox

#End Region

#Region "General"

        Public Sub Logout()

            Auth.HasAutoLogged = False

            Game.Shutdown()
            SelectChar.Init()

            Dim P As Packet
            P.PacketType = EvoLib.Protocol.LoginPacketType.CharList
            P.PacketOpCode = PacketOpCode.Enquiry
            P.Username = Globals.sUsername

            ClientSock.SendData(MakePacket(P, Nothing, Globals.Crypto))

        End Sub

        Public Sub Init()

            'If Constants.gSelectResX <> Constants.gLoginResX Or Constants.gSelectResY <> Constants.gLoginResY Then
            'DirectX.SetResoloution(Constants.gSelectResX, Constants.gSelectResY)
            'End If

            'SELECTION

            CharList = New SelCharList

            SelectWin = New GUI.MirWindow("winSelect", (Constants.gGameResX - Constants.gSelectResX) / 2, (Constants.gGameResY - Constants.gSelectResY) / 2, Constants.gSelectResX, Constants.gSelectResY)
            btnNew = New GUI.MirButton(SelectWin, "btnNew", 440, 93, 0.2, EvoLib.Protocol.ImgLibs.Interface1c, 53, 52, , , False)
            btnDelete = New GUI.MirButton(SelectWin, "btnDelete", 79, 243, 0.2, EvoLib.Protocol.ImgLibs.Interface1c, 55, 54, , , False)
            btnStart = New GUI.MirButton(SelectWin, "btnStart", 259, 49, 0.2, EvoLib.Protocol.ImgLibs.Interface1c, 57, 56, , , False)
            btnExit = New GUI.MirButton(SelectWin, "btnExit", 28, 438, 0.2, EvoLib.Protocol.ImgLibs.Interface1c, 59, 58, , , False)

            aSelChar1Shdw = New GUI.MirAnimation(SelectWin, "aSelChar1Shdw", 375, 210, False)
            aSelChar1Norm = New GUI.MirAnimation(SelectWin, "aSelChar1Norm", 375, 210, True)
            aSelChar1Magc = New GUI.MirAnimation(SelectWin, "aSelChar1Magc", 375, 210, False)

            aSelChar2Shdw = New GUI.MirAnimation(SelectWin, "aSelChar2Shdw", 230, 210, False)
            aSelChar2Norm = New GUI.MirAnimation(SelectWin, "aSelChar2Norm", 230, 210, True)
            aSelChar2Magc = New GUI.MirAnimation(SelectWin, "aSelChar2Magc", 230, 210, False)

            Dim tmpColors As New ArrayList
            tmpColors.Add(Color.FromArgb(128, 255, 255, 255))
            tmpColors.Add(Color.Goldenrod)
            tmpColors.Add(Color.LimeGreen)
            tmpColors.Add(Color.Crimson)
            tmpColors.Add(Color.Yellow)
            tmpColors.Add(Color.DeepSkyBlue)
            tmpColors.Add(Color.White)
            lblCharInfo = New GUI.MirText(SelectWin, "lblCharInfo", 79, 108, DirectX.ProFont9, "", Color.FromArgb(94, 160, 160, 160), Color.FromArgb(96, 96, 80), tmpColors, 6, 6)

            'CREATION
            CreateWin = New GUI.MirWindow("winCreate", (Constants.gGameResX - Constants.gSelectResX) / 2, (Constants.gGameResY - Constants.gSelectResY) / 2, Constants.gSelectResX, Constants.gSelectResY)

            lblClassInfo = New GUI.MirText(CreateWin, "lblClassInfo", 100, 31, DirectX.ProFont9, "", Color.FromArgb(128, 144, 144, 144), Color.FromArgb(255, 144, 144, 144), tmpColors, 6, 6, 438, 438)

            aCreateMaleShdw = New GUI.MirAnimation(CreateWin, "aCreateMaleShdw", 110, 100, False)
            aCreateFemaleShdw = New GUI.MirAnimation(CreateWin, "aCreateFemaleShdw", 400, 110, False)
            aCreateMaleNorm = New GUI.MirAnimation(CreateWin, "aCreateMaleNorm", 110, 100, True)
            aCreateFemaleNorm = New GUI.MirAnimation(CreateWin, "aCreateFemaleNorm", 400, 110, True)
            aCreateMaleMagc = New GUI.MirAnimation(CreateWin, "aCreateMaleMagc", 110, 100, False)
            aCreateFemaleMagc = New GUI.MirAnimation(CreateWin, "aCreateFemaleMagc", 400, 110, False)

            aCreateStoneShdw = New GUI.MirAnimation(CreateWin, "aCreateStoneShdw", 271, 400, False)
            aCreateStoneNorm = New GUI.MirAnimation(CreateWin, "aCreateStoneNorm", 271, 400, False)

            aCreateStoneShdw.StartAnim(New GUI.Animation(EvoLib.Protocol.ImgLibs.Interface1c, 83, 1, 60000, , Globals.ShadowBlend), True)
            aCreateStoneNorm.StartAnim(New GUI.Animation(EvoLib.Protocol.ImgLibs.Interface1c, 82, 1, 60000), True)

            btnClassWar = New GUI.MirButton(CreateWin, "btnClassWar", 260, 421, 0.2, EvoLib.Protocol.ImgLibs.Interface1c, 92, 93, 94, "Warrior", False)
            btnClassSor = New GUI.MirButton(CreateWin, "btnClassSor", 296, 421, 0.2, EvoLib.Protocol.ImgLibs.Interface1c, 95, 96, 97, "Sorcerer", False)
            btnClassTao = New GUI.MirButton(CreateWin, "btnClassTao", 332, 421, 0.2, EvoLib.Protocol.ImgLibs.Interface1c, 98, 99, 100, "Taoist", False)
            btnClassSin = New GUI.MirButton(CreateWin, "btnClassTao", 368, 421, 0.2, EvoLib.Protocol.ImgLibs.Interface1c, 101, 102, 103, "Assasin", False)

            btnCreateOK = New GUI.MirButton(CreateWin, "btnCreateOK", 450, 444, 0.2, EvoLib.Protocol.ImgLibs.Interface1c, 86, 87, 88, "Create", False)
            btnCreateCancel = New GUI.MirButton(CreateWin, "btnCreateCancel", 491, 444, 0.2, EvoLib.Protocol.ImgLibs.Interface1c, 89, 90, 91, "Cancel", False)

            txtCreateName = New GUI.MirTextBox(CreateWin, "txtCreateName", 282, 402, 12, EvoLib.Protocol.MAX_CHARNAME_LEN, DirectX.ProFont9, , "Character name", , , EvoLib.Protocol.VALID_CHARNAME_CHARS)

            Globals.gGameState = Globals.GameState.SelChar
            ShowSelect()

        End Sub

        Public Sub Shutdown()

            Windows.RemoveWindow(SelectWin)
            Windows.RemoveWindow(CreateWin)

            Sound.StopAllSounds()

            Libs.Interface1c.UnloadImages()
            Timers.RemoveAllTimers()

        End Sub

        Public Sub MsgBoxResultCallback(ByRef result As MessageBoxes.MsgBoxResult)

            Select Case result.mName

                Case Is = "NOCHARS"
                    ShowCreate()

                Case Is = "INVALIDNAME"
                    Windows.FocusInterface(txtCreateName)

                Case Is = "DOCREATE"

                    If result.mResult = MessageBoxes.MsgBoxResults.Yes Then

                        Dim Packet As EvoLib.Protocol.Packet
                        Packet.Username = Globals.sUsername
                        Packet.PacketType = EvoLib.Protocol.LoginPacketType.TryNewChar
                        Packet.PacketOpCode = EvoLib.Protocol.PacketOpCode.Enquiry

                        Dim struct As EvoLib.Protocol.CreateCharInfo
                        struct.STR_NAME = txtCreateName.Text
                        struct.BYTE_SEX = bCreateGender
                        struct.BYTE_JOB = bCreateClass

                        ClientSock.SendData(EvoLib.Protocol.MakePacket(Packet, struct, Globals.Crypto))

                        MessageBoxes.SetStateNotice("Creating character, please wait...")

                    Else
                        Windows.FocusInterface(txtCreateName)
                    End If

                Case Is = "DELCHAR"
                    If result.mInput.Length > 0 Then

                        Dim Packet As EvoLib.Protocol.Packet
                        Packet.Username = Globals.sUsername
                        Packet.PacketType = EvoLib.Protocol.LoginPacketType.TryDelChar
                        Packet.PacketOpCode = EvoLib.Protocol.PacketOpCode.Enquiry

                        Dim struct As EvoLib.Protocol.DelCharRequest
                        If CharList.BYTE_SELECTED = 0 Then
                            struct.ID_CHARACTER = CharList.CHAR_1.ID_CHARACTER
                        Else
                            struct.ID_CHARACTER = CharList.CHAR_2.ID_CHARACTER
                        End If
                        struct.STR_PASSMD5 = EvoLib.Protocol.Functions.MD5Hash(result.mInput)
                        ClientSock.SendData(EvoLib.Protocol.MakePacket(Packet, struct, Globals.Crypto))

                        MessageBoxes.SetStateNotice("Deleting character, please wait...")

                    End If

                Case Is = "DELETEOK"
                    GetChars()

                Case Is = "DELETEFAIL"

                Case Is = "EXIT"
                    If result.mResult = MessageBoxes.MsgBoxResults.Yes Then
                        Globals.bEnding = True
                    End If

            End Select

        End Sub

        Public Sub PacketArrival(ByRef info As EvoLib.Protocol.Packet, ByRef struct() As Byte)

            Select Case CType(info.PacketType, EvoLib.Protocol.LoginPacketType)

                Case EvoLib.Protocol.LoginPacketType.CharList
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Answer
                            CharList = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.SelCharList))
                            SetAnim(True)
                            MessageBoxes.RemoveStateNotice()

                            System.Windows.Forms.Application.DoEvents()

                            If Auth.HasAutoLogged Then

                                SelChar(True)
                            End If

                        Case EvoLib.Protocol.PacketOpCode.NegAcknowledge
                            'server has no chars on this account
                            MessageBoxes.AddMessageBox("NOCHARS", "You must create a character to begin playing", _
                                                        MessageBoxes.MsgBoxButtons.OK, AddressOf MsgBoxResultCallback)
                            MessageBoxes.RemoveStateNotice()
                        Case Else

                    End Select

                Case EvoLib.Protocol.LoginPacketType.TryNewChar
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.NegAcknowledge
                            Dim msg As EvoLib.Protocol.GenericMessage = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.GenericMessage))
                            MessageBoxes.AddMessageBox("CREATEFAIL", msg.MESSAGE, _
                                                        MessageBoxes.MsgBoxButtons.OK, AddressOf MsgBoxResultCallback)
                            MessageBoxes.RemoveStateNotice()
                        Case EvoLib.Protocol.PacketOpCode.Acknowledge
                            MessageBoxes.RemoveStateNotice()
                            ShowSelect()
                        Case Else

                    End Select

                Case EvoLib.Protocol.LoginPacketType.TryDelChar
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Acknowledge
                            MessageBoxes.AddMessageBox("DELETEOK", "Character deleted!", _
                                                        MessageBoxes.MsgBoxButtons.OK, AddressOf MsgBoxResultCallback)
                            MessageBoxes.RemoveStateNotice()

                        Case EvoLib.Protocol.PacketOpCode.NegAcknowledge
                            Dim msg As EvoLib.Protocol.GenericMessage = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.GenericMessage))
                            MessageBoxes.AddMessageBox("DELETEFAIL", msg.MESSAGE, _
                                                        MessageBoxes.MsgBoxButtons.OK, AddressOf MsgBoxResultCallback)
                            MessageBoxes.RemoveStateNotice()

                    End Select

                Case EvoLib.Protocol.LoginPacketType.TrySelectChar
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Acknowledge
                            Dim startinfo As EvoLib.Protocol.StartInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.StartInfo))
                            MiniMap.CurrentFmmapIndex = startinfo.LoadMapInfo.Fmmap_Index
                            GameWindow.LoadMap(startinfo.LoadMapInfo.Posinfo.STR_MAP)
                            GameWindow.mCurrentMapName = startinfo.LoadMapInfo.STR_MAPNAME
                            GameWindow.CoordX = startinfo.LoadMapInfo.Posinfo.INT2_X
                            GameWindow.CoordY = startinfo.LoadMapInfo.Posinfo.INT2_Y
                            Player.ShapeInfo = startinfo.ShapeInfo
                            Player.VitalPoints = startinfo.VitalPoints
                            SelectChar.Shutdown()
                            Game.Init()

                        Case EvoLib.Protocol.PacketOpCode.NegAcknowledge
                            MessageBoxes.RemoveStateNotice()
                            MessageBoxes.AddMessageBox("SELECTERROR", "Character selection failed!", MessageBoxes.MsgBoxButtons.OK, AddressOf MsgBoxResultCallback)

                    End Select

                Case EvoLib.Protocol.GamePacketType.DisconnectClient
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            MessageBoxes.AddMessageBox("DISCONNECT", "Disconnected from server due to lack of socket activity!", MessageBoxes.MsgBoxButtons.OK, AddressOf MsgBoxResultCallback)
                    End Select

                Case Else
                    Game.PacketArrival(info, struct)

            End Select

        End Sub

#End Region

#Region "Selection Screen"

        Private Sub ShowSelect()

            GetChars()

            Windows.RemoveWindow(CreateWin)
            Timers.RemoveAllTimers()
            Windows.AddWindow(SelectWin, Windows.WindowType.AlwaysBottom)

            SetAnim(True)

            Globals.IsInGame = False

            Sound.PlayBGM(Sound.GetBGMFile(Sound.BGM.SelectChar), 100)

        End Sub

        Private Sub GetChars()

            MessageBoxes.SetStateNotice("Retrieving characters...")

            CharList = Nothing

            Dim Packet As EvoLib.Protocol.Packet
            Packet.Username = Globals.sUsername
            Packet.PacketType = EvoLib.Protocol.LoginPacketType.CharList
            Packet.PacketOpCode = EvoLib.Protocol.PacketOpCode.Enquiry

            ClientSock.SendData(EvoLib.Protocol.MakePacket(Packet, , Globals.Crypto))

        End Sub

        Private Sub SelectWin_DrawWindow(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles SelectWin.DrawWindow

            If mode = Globals.DrawingMode.Normal Then

                With Libs.Interface1c

                    .Image(51).Draw(x, y, z)

                End With

            End If

        End Sub

        Private Sub aSelChar1_AnimStopped() Handles aSelChar1Norm.AnimStopped
            SetAnim(, 1)
        End Sub

        Private Sub aSelChar2_AnimStopped() Handles aSelChar2Norm.AnimStopped
            SetAnim(, 2)
        End Sub

        Private Sub aSelChar1_Click(ByVal name As String) Handles aSelChar1Norm.Click
            If CharList.BYTE_SELECTED = 1 And selAnimState1 <> 2 Then
                Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Click), 100)
                SetAnim()
            End If
        End Sub

        Private Sub aSelChar2_Click(ByVal name As String) Handles aSelChar2Norm.Click
            If CharList.BYTE_SELECTED = 0 And selAnimState2 <> 2 Then
                Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Click), 100)
                SetAnim()
            End If
        End Sub

        Private Sub btnExit_Click() Handles btnExit.Click
            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Click), 100)
            MessageBoxes.AddMessageBox("EXIT", "Do you really want to exit?", MessageBoxes.MsgBoxButtons.YesNo, AddressOf MsgBoxResultCallback)
        End Sub

        Private Sub btnStart_Click() Handles btnStart.Click

            SelChar()

        End Sub

        Public Sub SelChar(Optional auto As Boolean = False)

            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.ClickGo), 100)

            Dim Packet As EvoLib.Protocol.Packet
            Packet.Username = Globals.sUsername
            Packet.PacketType = EvoLib.Protocol.LoginPacketType.TrySelectChar
            Packet.PacketOpCode = EvoLib.Protocol.PacketOpCode.Enquiry

            Dim struct As EvoLib.Protocol.SelectCharRequest

            If auto Then

                If CharList.CHAR_2.STR_NAME = Globals.iniConfig.GetString("AUTOLOGIN", "CHAR", "") Then
                    struct.BYTE_SELECTED = 1
                    struct.ID_CHARACTER = CharList.CHAR_2.ID_CHARACTER
                    Player.PlayerID = CharList.CHAR_2.ID_CHARACTER
                    Player.PlayerName = CharList.CHAR_2.STR_NAME
                    Player.PlayerLevel = CharList.CHAR_2.INT2_LEVEL
                    Player.PlayerJob = CharList.CHAR_2.BYTE_JOB
                    Player.PlayerSex = CharList.CHAR_2.BYTE_SEX
                Else
                    struct.BYTE_SELECTED = 0
                    struct.ID_CHARACTER = CharList.CHAR_1.ID_CHARACTER
                    Player.PlayerID = CharList.CHAR_1.ID_CHARACTER
                    Player.PlayerName = CharList.CHAR_1.STR_NAME
                    Player.PlayerLevel = CharList.CHAR_1.INT2_LEVEL
                    Player.PlayerJob = CharList.CHAR_1.BYTE_JOB
                    Player.PlayerSex = CharList.CHAR_1.BYTE_SEX
                End If

            Else

                struct.BYTE_SELECTED = CharList.BYTE_SELECTED

                If CharList.BYTE_SELECTED = 0 Then
                    struct.ID_CHARACTER = CharList.CHAR_1.ID_CHARACTER
                    Player.PlayerID = CharList.CHAR_1.ID_CHARACTER
                    Player.PlayerName = CharList.CHAR_1.STR_NAME
                    Player.PlayerLevel = CharList.CHAR_1.INT2_LEVEL
                    Player.PlayerJob = CharList.CHAR_1.BYTE_JOB
                    Player.PlayerSex = CharList.CHAR_1.BYTE_SEX
                Else
                    struct.ID_CHARACTER = CharList.CHAR_2.ID_CHARACTER
                    Player.PlayerID = CharList.CHAR_2.ID_CHARACTER
                    Player.PlayerName = CharList.CHAR_2.STR_NAME
                    Player.PlayerLevel = CharList.CHAR_2.INT2_LEVEL
                    Player.PlayerJob = CharList.CHAR_2.BYTE_JOB
                    Player.PlayerSex = CharList.CHAR_2.BYTE_SEX
                End If

            End If

            ClientSock.SendData(EvoLib.Protocol.MakePacket(Packet, struct, Globals.Crypto))

            MessageBoxes.SetStateNotice("Logging in to game, and loading map...")

        End Sub

        Private Sub btnNew_Click() Handles btnNew.Click
            If CharList.CHAR_2.ID_CHARACTER = 0 Then
                ShowCreate()
                Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.ClickGo), 100)
            Else
                Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Click), 100)
                MessageBoxes.AddMessageBox("MAXCHARS", "You already have the maximum number of characters!", MessageBoxes.MsgBoxButtons.OK, AddressOf MsgBoxResultCallback)
            End If
        End Sub

        Private Sub btnDelete_Click() Handles btnDelete.Click

            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Click), 100)

            MessageBoxes.AddMessageBox("DELCHAR", "Are you sure you want to delete this character? It cannot be " & _
            "retrieved after and the name cannot be reused. If you are sure, please enter your password " & _
            "to continue.", MessageBoxes.MsgBoxButtons.Input, AddressOf MsgBoxResultCallback, _
            EvoLib.Protocol.MAX_PASSWORD_LEN, True)

        End Sub

        Private Sub SetAnim(Optional ByRef isreset As Boolean = False, Optional ByRef animstopped As Integer = 0)

            If CharList.CHAR_2.ID_CHARACTER = 0 Then
                aSelChar2Norm.StopAnim()
                aSelChar2Shdw.StopAnim()
                aSelChar2Magc.StopAnim()
                aSelChar2Norm.Visible = False
                aSelChar2Shdw.Visible = False
                aSelChar2Magc.Visible = False
            Else
                aSelChar2Norm.Visible = True
                aSelChar2Shdw.Visible = True
                aSelChar2Magc.Visible = True
            End If

            If CharList.CHAR_1.ID_CHARACTER = 0 Then
                aSelChar1Norm.StopAnim()
                aSelChar1Shdw.StopAnim()
                aSelChar1Magc.StopAnim()
                aSelChar1Norm.Visible = False
                aSelChar1Shdw.Visible = False
                aSelChar1Magc.Visible = False
                Exit Sub
            Else
                aSelChar1Norm.Visible = True
                aSelChar1Shdw.Visible = True
                aSelChar1Magc.Visible = True
            End If

            If isreset Then
                If CharList.BYTE_SELECTED = 1 And CharList.CHAR_2.ID_CHARACTER = 0 Then
                    CharList.BYTE_SELECTED = 0
                    selAnimState1 = 1
                    selAnimState2 = 0
                ElseIf CharList.BYTE_SELECTED = 1 Then
                    'CharList.BYTE_SELECTED = 1
                    selAnimState1 = 0
                    selAnimState2 = 1
                Else
                    CharList.BYTE_SELECTED = 0
                    selAnimState1 = 1
                    selAnimState2 = 0
                End If
            End If

            Dim do1 As Boolean, do2 As Boolean

            If animstopped = 0 Then
                do1 = True
                do2 = True
            ElseIf animstopped = 1 Then
                do1 = True
            Else
                do2 = True
            End If

            If do1 Then
                ApplyCharAnim(CharList.CHAR_1, selAnimState1, aSelChar1Norm, aSelChar1Shdw, aSelChar1Magc)
                If selAnimState1 = 2 Then CharList.BYTE_SELECTED = 0
                selAnimState1 += 1
                If selAnimState1 = 4 Then selAnimState1 = 0
            End If

            If do2 And CharList.CHAR_2.ID_CHARACTER > 0 Then
                ApplyCharAnim(CharList.CHAR_2, selAnimState2, aSelChar2Norm, aSelChar2Shdw, aSelChar2Magc)
                If selAnimState2 = 2 Then CharList.BYTE_SELECTED = 1
                selAnimState2 += 1
                If selAnimState2 = 4 Then selAnimState2 = 0
            End If

        End Sub

        Private Sub ApplyCharAnim(ByRef info As EvoLib.Protocol.SelCharInfo, ByRef state As Integer, ByRef norm As GUI.MirAnimation, ByRef shdw As GUI.MirAnimation, ByRef magc As GUI.MirAnimation)

            Dim animNorm As GUI.Animation
            Dim animShdw As GUI.Animation
            Dim animMagc As GUI.Animation
            Dim start As Integer
            Dim bloop As Boolean

            start = 201 + (info.BYTE_JOB * 600) + (info.BYTE_SEX * 300) + (state * 60)

            If state = 1 Then
                animNorm = New GUI.Animation(EvoLib.Protocol.ImgLibs.Interface1c, start, selanimCounts(info.BYTE_JOB, info.BYTE_SEX, state), cSelectAnimTick, Globals.DrawingMode.Normal, , selanimSounds(info.BYTE_JOB, info.BYTE_SEX))
            Else
                animNorm = New GUI.Animation(EvoLib.Protocol.ImgLibs.Interface1c, start, selanimCounts(info.BYTE_JOB, info.BYTE_SEX, state), cSelectAnimTick, Globals.DrawingMode.Normal)
            End If

            animShdw = New GUI.Animation(EvoLib.Protocol.ImgLibs.Interface1c, start + 20, selanimCounts(info.BYTE_JOB, info.BYTE_SEX, state), cSelectAnimTick, Globals.DrawingMode.Shadow)
            animMagc = New GUI.Animation(EvoLib.Protocol.ImgLibs.Interface1c, start + 40, selanimCounts(info.BYTE_JOB, info.BYTE_SEX, state), cSelectAnimTick, Globals.DrawingMode.Magic)

            bloop = (state = 0 Or state = 2)

            If state = 0 Then
                norm.StartAnim(animNorm, bloop)
                norm.mCurrentAnim.mDrawMode = Globals.DrawingMode.Grayscale
            Else
                norm.StartAnim(animNorm, bloop)
                norm.mCurrentAnim.mDrawMode = Globals.DrawingMode.Normal
            End If

            shdw.StartAnim(animShdw, bloop)
            magc.StartAnim(animMagc, bloop)

            If state = 1 Then
                lblCharInfo.TextString = "\0 Name:  \5\b" & info.STR_NAME & "\b\0\nLevel:  \b\4" & info.INT2_LEVEL & "\b\0\nClass:  \b\" & (info.BYTE_JOB + 1) & info.BYTE_JOB.ToString
            End If

        End Sub

#End Region

#Region "Creation Screen"

        Private Sub ShowCreate()

            Windows.RemoveWindow(SelectWin)
            Timers.RemoveAllTimers()
            Windows.AddWindow(CreateWin, Windows.WindowType.AlwaysBottom)

            bCreateScreen = True
            bCreateClass = EvoLib.Protocol.CharacterJobs.Warrior
            bCreateGender = EvoLib.Protocol.Gender.Male
            txtCreateName.Text = ""

            SetCreateAnims()

            Sound.PlayBGM(Sound.GetBGMFile(Sound.BGM.CreateChar), 100)

        End Sub

        Private Sub CreateWin_DrawWindow(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles CreateWin.DrawWindow

            If mode = Globals.DrawingMode.Normal Then
                'Background
                Libs.Interface1c.Image(81).Draw(x, y, z)
            End If

        End Sub

        Private Sub TryCreate()

            If txtCreateName.Text.Length = 0 Then
                MessageBoxes.AddMessageBox("INVALIDNAME", "You must enter a name for this character!", MessageBoxes.MsgBoxButtons.OK, AddressOf MsgBoxResultCallback)
            ElseIf txtCreateName.Text.Length < EvoLib.Protocol.MIN_CHARNAME_LEN Then
                MessageBoxes.AddMessageBox("INVALIDNAME", "Character name must be minimum " & EvoLib.Protocol.MIN_CHARNAME_LEN & " letters long!", MessageBoxes.MsgBoxButtons.OK, AddressOf MsgBoxResultCallback)

            Else

                MessageBoxes.AddMessageBox("DOCREATE", "Please confirm your selection:" & vbCrLf & vbCrLf & _
                bCreateGender.ToString & " " & bCreateClass.ToString & " called " & txtCreateName.Text & vbCrLf & vbCrLf & _
                "is this correct?", MessageBoxes.MsgBoxButtons.YesNo, AddressOf MsgBoxResultCallback)

            End If

        End Sub

        Private Sub btnCreateCancel_Click() Handles btnCreateCancel.Click
            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.ClickGo), 100)
            ShowSelect()
        End Sub

        Private Sub btnCreateOK_Click() Handles btnCreateOK.Click
            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Click), 100)
            TryCreate()
        End Sub

        Private Sub btnClassWar_Click() Handles btnClassWar.Click
            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Click), 100)
            If bCreateClass = EvoLib.Protocol.CharacterJobs.Warrior Then Exit Sub
            bCreateClass = EvoLib.Protocol.CharacterJobs.Warrior
            SetCreateAnims()
        End Sub

        Private Sub btnClassSor_Click() Handles btnClassSor.Click
            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Click), 100)
            If bCreateClass = EvoLib.Protocol.CharacterJobs.Sorcerer Then Exit Sub
            bCreateClass = EvoLib.Protocol.CharacterJobs.Sorcerer
            SetCreateAnims()
        End Sub

        Private Sub btnClassTao_Click() Handles btnClassTao.Click
            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Click), 100)
            If bCreateClass = EvoLib.Protocol.CharacterJobs.Taoist Then Exit Sub
            bCreateClass = EvoLib.Protocol.CharacterJobs.Taoist
            SetCreateAnims()
        End Sub

        Private Sub btnClassSin_Click() Handles btnClassSin.Click
            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Click), 100)
            If bCreateClass = EvoLib.Protocol.CharacterJobs.Assassin Then Exit Sub
            bCreateClass = EvoLib.Protocol.CharacterJobs.Assassin
            SetCreateAnims()
        End Sub

        Private Sub aCreateMaleNorm_Click(ByVal name As String) Handles aCreateMaleNorm.Click
            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Click), 100)
            If bCreateGender = EvoLib.Protocol.Gender.Male Then Exit Sub
            bCreateGender = EvoLib.Protocol.Gender.Male
            SetCreateAnims()
        End Sub

        Private Sub aCreateFemaleNorm_Click(ByVal name As String) Handles aCreateFemaleNorm.Click
            Sound.PlaySoundEffect(Sound.GetSoundEffectFile(Sound.SoundEffects.Click), 100)
            If bCreateGender = EvoLib.Protocol.Gender.Female Then Exit Sub
            bCreateGender = EvoLib.Protocol.Gender.Female
            SetCreateAnims()
        End Sub

        Private Sub SetCreateAnims()

            If bCreateGender = EvoLib.Protocol.Gender.Male Then
                ApplyCreateAnim(bCreateClass, EvoLib.Protocol.Gender.Male, False, aCreateMaleNorm, aCreateMaleShdw, aCreateMaleMagc)
                ApplyCreateAnim(bCreateClass, EvoLib.Protocol.Gender.Female, True, aCreateFemaleNorm, aCreateFemaleShdw, aCreateFemaleMagc)
            Else
                ApplyCreateAnim(bCreateClass, EvoLib.Protocol.Gender.Male, True, aCreateMaleNorm, aCreateMaleShdw, aCreateMaleMagc)
                ApplyCreateAnim(bCreateClass, EvoLib.Protocol.Gender.Female, False, aCreateFemaleNorm, aCreateFemaleShdw, aCreateFemaleMagc)
            End If

            Dim tmp As String = "\b\" & (bCreateClass + 1) & "[" & bCreateGender.ToString & " " & bCreateClass.ToString & "]\n\n\b\6"
            Select Case bCreateClass

                Case EvoLib.Protocol.CharacterJobs.Warrior
                    tmp &= sCreateWarText
                Case EvoLib.Protocol.CharacterJobs.Taoist
                    tmp &= sCreateTaoText
                Case EvoLib.Protocol.CharacterJobs.Sorcerer
                    tmp &= sCreateSorText
                Case EvoLib.Protocol.CharacterJobs.Assassin
                    tmp &= sCreateSinText

            End Select

            lblClassInfo.TextString = tmp

        End Sub

        Private Sub ApplyCreateAnim(ByRef charclass As EvoLib.Protocol.CharacterJobs, ByRef gender As EvoLib.Protocol.Gender, ByRef isstone As Boolean, ByRef aNorm As GUI.MirAnimation, ByRef aShdw As GUI.MirAnimation, ByRef aMagc As GUI.MirAnimation)

            Dim start As Integer
            If charclass = CharacterJobs.Assassin Then
                start = 541 + (charclass * 600) + (gender * 300)
                If gender = gender.Male Then
                    aNorm.SetPos(110, 280)
                    aShdw.SetPos(110, 280)
                    aMagc.SetPos(110, 280)
                Else
                    aNorm.SetPos(440, 290)
                    aShdw.SetPos(440, 290)
                    aMagc.SetPos(440, 290)
                End If
                btnCreateOK.Visible = False
            Else
                start = 441 + (charclass * 600) + (gender * 300)
                If gender = gender.Male Then
                    aNorm.SetPos(110, 100)
                    aShdw.SetPos(110, 100)
                    aMagc.SetPos(110, 100)
                Else
                    aNorm.SetPos(400, 110)
                    aShdw.SetPos(400, 110)
                    aMagc.SetPos(400, 110)
                End If
                btnCreateOK.Visible = True
            End If
            Dim yoff As Integer = 0
            If gender = EvoLib.Protocol.Gender.Female And charclass = EvoLib.Protocol.CharacterJobs.Warrior Then yoff = 50

            Dim animshdw As GUI.Animation
            Dim animnorm As GUI.Animation
            Dim animmagc As GUI.Animation

            If isstone Then
                animshdw = New GUI.Animation(EvoLib.Protocol.ImgLibs.Interface1c, start + 20, 1, cCreateAnimTick, Globals.DrawingMode.Shadow, , , , yoff)
                animnorm = New GUI.Animation(EvoLib.Protocol.ImgLibs.Interface1c, start, 1, cCreateAnimTick, Globals.DrawingMode.Grayscale, , , , yoff)
                animmagc = New GUI.Animation(EvoLib.Protocol.ImgLibs.Interface1c, 0, 1, cCreateAnimTick, Globals.DrawingMode.Magic, , , , yoff)
            Else
                animshdw = New GUI.Animation(EvoLib.Protocol.ImgLibs.Interface1c, start + 20, createanimCounts(charclass, gender), cCreateAnimTick, Globals.DrawingMode.Shadow, , , , yoff)
                animnorm = New GUI.Animation(EvoLib.Protocol.ImgLibs.Interface1c, start, createanimCounts(charclass, gender), cCreateAnimTick, , , createanimSounds(charclass, gender), , yoff)
                animmagc = New GUI.Animation(EvoLib.Protocol.ImgLibs.Interface1c, start + 40, createanimCounts(charclass, gender), cCreateAnimTick, Globals.DrawingMode.Magic, , , , yoff)
            End If


            aShdw.StartAnim(animshdw, True)
            aNorm.StartAnim(animnorm, True)
            aMagc.StartAnim(animmagc, True)

        End Sub

#End Region

    End Module

End Namespace
